package cn.iocoder.yudao.module.project.service.kisbatchcode;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.kisbatchcode.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kisbatchcode.KisBatchCodeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.kisbatchcode.KisBatchCodeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 批次码 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KisBatchCodeServiceImpl implements KisBatchCodeService {

    @Resource
    private KisBatchCodeMapper kisBatchCodeMapper;

    @Override
    public String createKisBatchCode(KisBatchCodeSaveReqVO createReqVO) {
        // 插入
        KisBatchCodeDO kisBatchCode = BeanUtils.toBean(createReqVO, KisBatchCodeDO.class);
        kisBatchCodeMapper.insert(kisBatchCode);
        // 返回
        return kisBatchCode.getId();
    }

    @Override
    public void updateKisBatchCode(KisBatchCodeSaveReqVO updateReqVO) {
        // 校验存在
        validateKisBatchCodeExists(updateReqVO.getId());
        // 更新
        KisBatchCodeDO updateObj = BeanUtils.toBean(updateReqVO, KisBatchCodeDO.class);
        kisBatchCodeMapper.updateById(updateObj);
    }

    @Override
    public void deleteKisBatchCode(String id) {
        // 校验存在
        validateKisBatchCodeExists(id);
        // 删除
        kisBatchCodeMapper.deleteById(id);
    }

    private void validateKisBatchCodeExists(String id) {
        if (kisBatchCodeMapper.selectById(id) == null) {
            throw exception(KIS_BATCH_CODE_NOT_EXISTS);
        }
    }

    @Override
    public KisBatchCodeDO getKisBatchCode(String id) {
        return kisBatchCodeMapper.selectById(id);
    }

    @Override
    public PageResult<KisBatchCodeDO> getKisBatchCodePage(KisBatchCodePageReqVO pageReqVO) {
        return kisBatchCodeMapper.selectPage(pageReqVO);
    }

}